package com.itbaizhan.hbase2hdfs;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class Hbase2HdfsReducer extends Reducer<Text, IntWritable,Text,IntWritable> {
    //定义输出的value对象,不在reduce方法中定义的原因是可以减少垃圾对象的产生
    private IntWritable valOut = new IntWritable();
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values,
                          Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        //定义当前key代表的单词出现的总次数
        int sum = 0;
        //遍历values
        for(IntWritable value:values){
            sum += value.get();
        }
        //将sum的值封装到valOut对象中
        valOut.set(sum);
        //输出:hello 100000
        context.write(key,valOut);
    }
}
